package com.example.demo.controller;

import com.example.demo.entity.Product;
import com.example.demo.service.ProductService;
import org.springframework.web.bind.annotation.RestController;
import java.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/Product")
public class ProductController {
    @Autowired
    ProductService Service;


    @GetMapping("")
    public ResponseEntity<List<Product>> getProduct(){
        List<Product> result = Service.findProduct();
        return ResponseEntity.ok(result);
    }


    @GetMapping("/{id}")
    public ResponseEntity<Product> findProduct(@PathVariable int id){
        Product result = Service.getProduct(id);
        if(result == null){return ResponseEntity.noContent().build();}
        else{return ResponseEntity.ok(result);}
    }


    @PostMapping("")
    public ResponseEntity<Product> addProduct(@RequestBody Product p){
        Product result = Service.addProduct(p);
        return ResponseEntity.ok(result);}


    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteProduct(@PathVariable int id){
        Service.deleteProduct(id);
        return ResponseEntity.ok().build();
    }


    @PutMapping("/{id}")
    public ResponseEntity<Void> putProduct(@PathVariable int id, @RequestBody Product p){
        Service.updateProduct(id, p);
        return ResponseEntity.ok().build();
    }
}

